//
//  HotRecommendTableViewCell.m
//  HimalayaMusic
//
//  Created by dllo on 16/1/7.
//  Copyright © 2016年 WXCLegendary. All rights reserved.
//

#import "HotRecommendTableViewCell.h"

@implementation HotRecommendTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.buttonArray = [NSMutableArray array];
        self.labelArray = [NSMutableArray array];
        self.alphaLabelArray = [NSMutableArray array];
        [self createView];
        
        self.leftLabel = [[UILabel alloc] init];
        self.leftLabel.textAlignment = NSTextAlignmentCenter;
        self.leftLabel.font = [UIFont systemFontOfSize:14];
        self.rightButton = [UIButton buttonWithType:UIButtonTypeSystem];
        self.topView = [[UIView alloc] init];

        
        [self.topView addSubview:self.leftLabel];
        [self.topView addSubview:self.rightButton];
        [self.rightButton setTitle:@"更多" forState:UIControlStateNormal];
        [self.rightButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [self.rightButton addTarget:self action:@selector(moreAction:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:self.topView];
        self.topView.backgroundColor = [UIColor colorWithRed:206 / 255.0 green:71 / 255.0 blue:56 / 255.0 alpha:1];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeColor) name:@"nightMode" object:nil];
    }
    

    return self;
}

//夜间模式通知调用的方法
- (void)changeColor {
    NSString *judge = [[NSUserDefaults standardUserDefaults] valueForKey:@"nightMode"];
    for (int i = 0; i < self.labelArray.count; i++) {
        UILabel *label = (UILabel *)self.labelArray[i];
        if ([judge isEqualToString:@"night"]) {
            label.backgroundColor = [UIColor colorWithRed:29 / 255.0 green:40 / 255.0 blue:52 / 255.0 alpha:1];
            label.textColor = [UIColor whiteColor];
            self.backgroundColor = [UIColor colorWithRed:29 / 255.0 green:40 / 255.0 blue:52 / 255.0 alpha:1];
        } else {
            label.backgroundColor = [UIColor whiteColor];
            label.textColor = [UIColor blackColor];
            self.backgroundColor = [UIColor clearColor];
        }
    }
}

- (void)moreAction:(UIButton *)sender {

    [sender setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    
    
    [[NSNotificationCenter defaultCenter] postNotificationName:@"push" object:self.hotRecommendModel.categoryId];
    
}




- (void)createView {
    for (int i = 0; i < 3; i++) {
        UIImageView *imageView = [[UIImageView alloc] init];
        imageView.tag = 100 + i;
        [self addSubview:imageView];
        [self.buttonArray addObject:imageView];
        
        imageView.userInteractionEnabled = YES;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
        [imageView addGestureRecognizer:tap];
        
        
        
        
        UILabel *label = [[UILabel alloc] init];
        label.numberOfLines = 0;
        label.font = [UIFont systemFontOfSize:12];

        [self addSubview:label];
        [self.labelArray addObject:label];
        
        UILabel *alphaLabel = [[UILabel alloc] init];
        alphaLabel.font = [UIFont systemFontOfSize:12];
        alphaLabel.textAlignment = NSTextAlignmentCenter;
        alphaLabel.backgroundColor = [UIColor blackColor];
        alphaLabel.alpha = 0.7;
        alphaLabel.textColor = [UIColor whiteColor];
        [self addSubview:alphaLabel];
        [self.alphaLabelArray addObject:alphaLabel];
    

        
    }
    

    


    
}

- (void)layoutSubviews {
    [super layoutSubviews];
    self.topView.frame = CGRectMake(0, 0, self.width, 40);
    self.leftLabel.frame = CGRectMake(0, 0, 100, 40);
    self.rightButton.frame = CGRectMake(self.width - 100, 0, 100, 40);
    for (int i = 0; i < 3; i++) {
        UIImageView *imageView = self.buttonArray[i];
        imageView.frame = CGRectMake(((self.width - 20) / 3 + 5)* i + 5, self.topView.bottom + 5, (self.width - 20) / 3, self.height - 80);
        UILabel *label = (UILabel *)self.labelArray[i];
        label.frame = CGRectMake(((self.width - 20) / 3 + 5)* i + 5, imageView.bottom, (self.width - 20) / 3, self.height - self.topView.height - imageView.height - 10);
        

        
        UILabel *alphaLabel = (UILabel *)self.alphaLabelArray[i];
        alphaLabel.frame = CGRectMake(((self.width - 20) / 3 + 5)* i + 5, imageView.bottom - 20, (self.width - 20) / 3, 20);
        
    }
    
    
}

- (void)setHotRecommendModel:(HotRecommendsModel *)hotRecommendModel {
    if (_hotRecommendModel != hotRecommendModel) {
        _hotRecommendModel = hotRecommendModel;
        
    }
    for (int i = 0; i < 3; i++) {
        UIImageView *imageView = self.buttonArray[i];
        [imageView sd_setImageWithURL:[NSURL URLWithString:[self.hotRecommendModel.listArray[i] coverLarge]]];
        
        UILabel *label = (UILabel *)self.labelArray[i];
        label.text = [self.hotRecommendModel.listArray[i] trackTitle];
        
        UILabel *alphaLabel = (UILabel *)self.alphaLabelArray[i];
        alphaLabel.text = [self.hotRecommendModel.listArray[i] title];
        
        
        
    }


    self.leftLabel.text = self.hotRecommendModel.title;
    [self changeColor];
    
}

- (void)tapAction:(UITapGestureRecognizer *)tap {

   HotRecommendListModel *model = self.hotRecommendModel.listArray[tap.view.tag - 100];
    [self.delegate passValue:model];

}






- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
